﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GraphicsEditor
{
    public partial class GraphicsEditorForm : Form
    {
        ToolboxUserControl toolbox;

        
        

        public GraphicsEditorForm()
        {
            InitializeToolbox();
            InitializeComponent();
        }

        private void InitializeToolbox()
        {
            toolbox = new ToolboxUserControl();
            toolbox.Dock = DockStyle.Left;
            Controls.Add(toolbox);
            this.MdiChildActivate += new EventHandler(toolbox.ActiveChildChanged);
        }

        private void GraphicsEditorForm_Load(object sender, EventArgs e)
        {
           
            newToolStripMenuItem.PerformClick();
            ((ScrollableCanvasForm)ActiveMdiChild).WindowState = FormWindowState.Maximized;

        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddScrollableCanvasForm("", GraphicsEditor.CANVAS_SIZE);
        }

        private ScrollableCanvasForm AddScrollableCanvasForm(string fileName, Size size)
        {
            ScrollableCanvasForm scrollableCanvasForm = new ScrollableCanvasForm(fileName, size);
            scrollableCanvasForm.MdiParent = this;
            scrollableCanvasForm.Show();
            return scrollableCanvasForm;
            //scrollableCanvasForm.DrawingToolRequested += new ScrollableCanvasForm.DrawingToolRequestHandler(UpdateDrawingToolsForActiveChild);
        }


        private Bitmap CreateBlankBitmap()
        {
            Bitmap bitmap = new Bitmap(GraphicsEditor.CANVAS_SIZE.Width, GraphicsEditor.CANVAS_SIZE.Height);
            SolidBrush brush = new SolidBrush(GraphicsEditor.CANVAS_COLOR);
            Graphics graphics = Graphics.FromImage(bitmap);

            graphics.FillRectangle(brush, 0, 0, GraphicsEditor.CANVAS_SIZE.Width, GraphicsEditor.CANVAS_SIZE.Height);
            brush.Dispose();
            graphics.Dispose();

            return bitmap;
        }

        private void openFromFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog.Filter = GraphicsEditor.FILE_FILTER;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string fileName = openFileDialog.FileName;
                ScrollableCanvasForm child = AddScrollableCanvasForm(fileName,GraphicsEditor.CANVAS_SIZE);
                child.Open(fileName);
            }
        }

        private void openFromWebToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
        }

        private void fileToolStripMenuItem_DropDownOpened(object sender, EventArgs e)
        {
            bool enable = MdiChildren.Count() > 0 ? true : false;

            saveToolStripMenuItem.Enabled = enable;
            saveAsToolStripMenuItem.Enabled = enable;
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ScrollableCanvasForm activeChild = (ScrollableCanvasForm)ActiveMdiChild;

            if (activeChild.FileName == "" || activeChild.FileName == null)
            {
                saveAsToolStripMenuItem.PerformClick();
            }
            else {
                activeChild.Save(activeChild.FileName);
            }
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ScrollableCanvasForm activeChild = (ScrollableCanvasForm)ActiveMdiChild;

            saveFileDialog.FileName = activeChild.FileName;
            saveFileDialog.Filter = GraphicsEditor.FILE_FILTER;

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                activeChild.FileName = saveFileDialog.FileName;
                activeChild.Save(activeChild.FileName);
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MdiChildren.Length > 0)
                (ActiveMdiChild as ScrollableCanvasForm).CopyShape();
        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MdiChildren.Length > 0)
                (ActiveMdiChild as ScrollableCanvasForm).PasteShape();
        }
             

    }

}
